<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
require_once("conn.php");

$www = getDomain()."admin/";


$image_file = $_FILES ['imageURL'] ['name'];
if ($image_file != "") {
    $path_parts = pathinfo($image_file);
    $filename = "upload/" . date("YmdHis") . "." . $path_parts ['extension'];
    move_uploaded_file($_FILES ['imageURL'] ['tmp_name'], $filename);
    $imageURL = $www . $filename;
}
$flashid = checkTrim($_POST['flashid']);
$flashcode = checkTrim($_POST['flashcode']);
$name = checkTrim($_POST['name']);
$url = checkTrim($_POST['url']);
$fp = fopen($url, "r");
if ($fp) {
    $a_array = get_headers($url, true);
    $size = round($a_array['Content-Length'] / (1024 * 1000), 3);
} else {
    echo $url . " 文件不存在！";
}

$version = checkTrim($_POST['version']);
$age = checkTrim($_POST['age']);
$isModify = $_POST['isModify'];

$type1 = $_POST['type1'];
$type2 = $_POST['type2'];

$v = "";
if (is_array($type2) && count($type2) > 0) {
    foreach ($type2 as $t) {
        $v = $v . "," . $t;
    }
} else {
    $v = $type2;
}
if (strlen($v) > 0) {
    if (substr($v, 0, 1) == ',') {
        $v = substr($v, 1);
    }
}

if (isset($_POST['packid']) && $_POST['packid'] != "") {
    $packid = $_POST['packid'];
} else {
    $packid = -1;
}
$db = new DBC();
$is_code_exist = false;
if ($isModify == 1) {
    if ($image_file == "") {
        $sql = "UPDATE t_product_en SET flashcode = '" . $flashcode . "',name = '" . $name . "',age='" . $age . "',url = '" . $url . "',version='" . $version . "',size='" . $size . "',type1 = '" . $type1 . "',type2 = '" . $v . "',packid = " . $packid . " WHERE productid = " . $flashid;
    } else {
        $sql = "UPDATE t_product_en SET flashcode = '" . $flashcode . "',name = '" . $name . "',age='" . $age . "',url = '" . $url . "',imageURL = '" . $imageURL . "',version='" . $version . "',size='" . $size . "',type1 = '" . $type1 . "',type2 = '" . $v . "',packid = " . $packid . " WHERE productid = " . $flashid;
    }
} else {
    $sql = "SELECT flashcode FROM t_product_en WHERE flashcode= '" . $flashcode . "'";
    $data = $db->fetchData($sql);
    if ($data != null && count($data) > 0) {
        $is_code_exist = true;
    }
    $sql = "INSERT INTO t_product_en(flashcode,name,age,url,imageURL,version,size,type1,type2,packid,createdate) VALUES('" . $flashcode . "','" . $name . "'," . $age . ",'" . $url . "'," . $imageURL . "','" . $version . "','" . $size . "','" . $type1 . "','" . $v . "'," . $packid . ",now())";
}

if ($is_code_exist) {
    $msg = "编号 " . $flashcode . " 已经存在。";
    echo "<script language='javascript'>alert('" . $msg . "'); </script>";
    echo "<script language='javascript'>history.back();</script>";
} else {
    echo $sql;
    $transfer = "fileEnList.php?menu=apk";
    if ($db->execute($sql)) {
        //success
        //    echo "<script language='javascript'>alert('成功'); </ script>";
        echo "<script language='javascript'>location='" . $transfer . "';</script>";
    } else {
        //failue
        echo "<script language='javascript'>alert('失败'); </script>";
        echo "<script language='javascript'>history.back();</script>";
    }
}

function checkTrim($item) {
    if (isset($item)) {
        $item = trim($item);
    } else {
        $item = "";
    }
    return $item;
}
?>
